package cn.tedu.loding.service.impl;

import cn.tedu.loding.ex.ServiceException;
import cn.tedu.loding.mapper.SkuMapper;
import cn.tedu.loding.pojo.dto.SkuAddNewDTO;
import cn.tedu.loding.pojo.entity.Attribute;
import cn.tedu.loding.pojo.entity.Sku;
import cn.tedu.loding.pojo.vo.SkuListItemVO;
import cn.tedu.loding.pojo.vo.SkuSpecificationStandardVO;
import cn.tedu.loding.pojo.vo.SkuStandardVO;
import cn.tedu.loding.service.ISkuService;
import cn.tedu.loding.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class SkuServiceImpl implements ISkuService {

    @Autowired
    SkuMapper skuMapper;

    @Override
    public void addNew(SkuAddNewDTO skuAddNewDTO) {
        log.debug("开始处理【添加sku】业务,参数为{}",skuAddNewDTO);





        Sku sku=new Sku();
        BeanUtils.copyProperties(skuAddNewDTO,sku);
        sku.setAlbumId(5L);
        sku.setTitle("qiiq");
        log.debug("名称未占用,将插入数据{}",sku);

        int count = skuMapper.insert(sku);
        if (count!=1){
            String message = "服务器繁忙,稍后再试";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }
        log.debug("插入成功,数据为{}",sku);

    }

    @Override
    public void delete(Long id) {
        log.debug("开始根据id【{}】处理删除sku操作",id);
        {
            SkuStandardVO result = skuMapper.getStandardById(id);
            if (result == null) {
                String message = "该sku不存在,删除失败";
                log.debug(message);
                throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
            }
        }


        int count = skuMapper.deleteById(id);
        if (count != 1) {
            String message = "服务器繁忙,稍后再试";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        log.debug("删除成功!");
    }

    @Override
    public List<SkuListItemVO> list() {
        log.debug("开始处理【sku列表】业务,无参数");
        return skuMapper.list();
    }
}
